package com.gopay.scheduler.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.domain.acps.AcctMastField;
import com.gopay.common.user.dao.UserDailyBalStatsDAO;
import com.gopay.remote.acps.service.AcctTransRemoteService;
import com.gopay.remote.user.UserRemoteService;
import com.gopay.scheduler.GopayBaseJob;

/**
 * 每日日终状态清零任务。
 * 
 * 在此任务中，主要进行以下的状态清零工作： 1. 对密码输入错误次数过多的企业或个人客户，重置密码输入错误，并恢复用户状态为正常； 2.
 * 对每日提现限额进行清零操作。
 * 
 * @author 孙阳
 */
public class DailyResetStateJob extends GopayBaseJob {
	/* 注入数据声明开始 */
	private UserRemoteService userOperClient;
	private AcctTransRemoteService transService;
	private UserDailyBalStatsDAO userDailyBalStatsDao;
	/* 注入数据声明结束 */

	private Logger logger = LoggerFactory.getLogger(DailyResetStateJob.class);

	/**
	 * Spring Quartz强制实现方法 此方法包含定时执行任务中的业务逻辑 1. 清空密码错误次数，更改用户密码锁定状态至正常状态。
	 */
	public void doJobInternal() {

		logger.info("in the reset...");
		userOperClient.clearUserInfo();

		int count = userDailyBalStatsDao.getCount();
		if (count == 0) {
			return;
		}
		// 循环次数
		int i = (count + 999) / 1000;
		for (int j = 0; j < i; j++) {
			List<Object> accts = userDailyBalStatsDao.getAccts(j * 1000 + 1,
					1000);
			clearclearDailyLimit(accts);
		}
		logger.info(" reset task to complete.......");

	}

	private void clearclearDailyLimit(List<Object> accts) {
		Map<AcctMastField, String> map = new HashMap<AcctMastField, String>();
		map.put(AcctMastField.TODAY_PWD_ERR_NUM, "0");
		for (Object object : accts) {
			try {
				String acctId = String.valueOf(object);
				transService.updateAcctMast(acctId, map);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Resource(name = "userOperClient")
	public void setUserOperClient(UserRemoteService userOperClient) {
		this.userOperClient = userOperClient;
	}

	@Resource(name = "transServiceClient")
	public void setTransService(AcctTransRemoteService transService) {
		this.transService = transService;
	}

	@Resource(name="userDailyBalStatsDao")
	public void setUserDailyBalStatsDao(UserDailyBalStatsDAO userDailyBalStatsDao) {
		this.userDailyBalStatsDao = userDailyBalStatsDao;
	}

}
